
define(['ionic'], function () {
    angular.module('BUSContacts.utility', [])
        .constant('APPCONTANTS', {
            getDepartmentListURL: 'http://upsocial.yonyou.com/openapi/department/getlist',
            getUserListURL: 'http://upsocial.yonyou.com/openapi/user/getlist',

            getDepOperaterInfoCmd: 'UFIDA.U9.ISV.MFGRestContactSV.Contact.IGetDepOperaterInfo',
            getFriendsCmd: 'UFIDA.U9.ISV.MFGRestContactSV.Contact.IGetFriends',
            managerFriendsCmd: 'UFIDA.U9.ISV.MFGRestContactSV.Contact.IAddOrDelFriendSv',
            queryOperaterInfoCmd: 'UFIDA.U9.ISV.MFGRestContactSV.Contact.IQueryOperInfo',
            queryOperatersCmd: 'UFIDA.U9.ISV.MFGRestContactSV.Contact.IQueryOperInfo'
        })
        .factory('UtilService', ['ionicU9Tips', function (ionicU9Tips) {
            var o = {};

            o.tipsTop = function (tips) {
                ionicU9Tips.show({
                    content: tips,
                    position: 'top',
                    duration: 1500,
                    dismissable: true
                });
            };
            o.tipsCenter = function (tips) {
                ionicU9Tips.show({
                    content: tips,
                    position: 'center',
                    duration: 1500,
                    dismissable: true
                });
            };
            o.tipsBottom = function (tips) {
                ionicU9Tips.show({
                    content: tips,
                    position: 'bottom',
                    duration: 1500,
                    dismissable: true
                });
            };

            return o;
        }])
        .factory('OperaterUtil', ['$q', '$http', 'APPCONTANTS', 
            function ($q, $http, APPCONTANTS) {
                var o = {};

                o.queryDepOperater = function (userCode) {
                    var defer = $q.defer();

                    u9.callSVAlone({
                        name: APPCONTANTS.getDepOperaterInfoCmd,
                        params: [userCode],
                        onSuccess: function (data) {
                            defer.resolve(data);
                        },
                        onFailure: function () {
                            defer.reject();
                        }
                    });

                    return defer.promise;
                };
                o.queryOperaterFriends = function (userId) {
                    var defer = $q.defer();

                    u9.callSVAlone({
                        name: APPCONTANTS.getFriendsCmd,
                        params: [userId],
                        onSuccess: function (data) {
                            defer.resolve(data);
                        },
                        onFailure: function () {
                            defer.reject();
                        }
                    });

                    return defer.promise;
                };
                o.managerFriends = function (userId, friendId, isAdd) {
                    var defer = $q.defer();

                    u9.callSVAlone({
                        name: APPCONTANTS.managerFriendsCmd,
                        params: [userId, friendId, isAdd],
                        onSuccess: function (data) {
                            defer.resolve(data);
                        },
                        onFailure: function () {
                            defer.reject();
                        }
                    });

                    return defer.promise;
                };
                o.queryOperaters = function (keyword, isQueryFriend, userId) {
                    var defer = $q.defer();

                    u9.callSVAlone({
                        name: APPCONTANTS.queryOperatersCmd,
                        params: [keyword, isQueryFriend, userId],
                        onSuccess: function (data) {
                            defer.resolve(data);
                        },
                        onFailure: function () {
                            defer.reject();
                        }
                    });

                    return defer.promise;
                };

                return o;
            }
        ])
        .factory('WeChatUtil', ['$q', '$http', 'APPCONTANTS', function ($q, $http, APPCONTANTS) {
            var o = {};

            o.getDepartmentList = function (company, currentPage, itemsPerPage) {
                var defer = $q.defer();

                $http({
                    method: 'POST',
                    url: APPCONTANTS.getDepartmentListURL,
                    data: {
                        key_temp: 'abc',//faultylabs.MD5(company),
                        currentPage: currentPage,
                        itemsPerPage: itemsPerPage
                    },
                    timeout: 5000
                }).success(function (data) {
                    if (data.error_code === "000000") {
                        defer.resolve(data.data);
                    } else {
                        defer.reject();
                    }
                }).error(function () {
                    defer.reject();
                });

                return defer.promise;
            };
            o.getUserList = function (company, currentPage, itemsPerPage) {
                var defer = $q.defer();

                $http({
                    method: 'POST',
                    url: APPCONTANTS.getUserListURL,
                    data: {
                        key_temp: 'abc',//faultylabs.MD5(company),
                        currentPage: currentPage,
                        itemsPerPage: itemsPerPage
                    },
                    timeout: 5000
                }).success(function (data) {
                    if (data.error_code === "000000") {
                        defer.resolve(data.data);
                    } else {
                        defer.reject();
                    }
                }).error(function () {
                    defer.reject();
                });

                return defer.promise;
            };

            return o;
        }])
        .factory('cordovaCall', ['$q', function ($q) {
            return {
                call: function (number) {
                    var q = $q.defer();
                    if (u9.plugins.CallNumber) {
                        u9.plugins.CallNumber.callNumber(function (res) {
                            q.resolve(res);
                        }, function (err) {
                            q.reject(err);
                        }, number);
                    } else {
                        q.reject();
                    }

                    return q.promise;
                }
            };

        }])
        .factory('cordovaSms', ['$q', function ($q) {
            return {
                send: function (number, message, options) {
                    var q = $q.defer();
                    if (u9.plugins.sms) {
                        u9.plugins.sms.send(number, message, options, function (res) {
                            q.resolve(res);
                        }, function (err) {
                            q.reject(err);
                        });
                    } else {
                        q.reject();
                    }

                    return q.promise;
                }
            };

        }])
        .factory('cordovaEmailComposer', ['$q', function ($q) {
            return {
                isAvailable: function () {
                    var q = $q.defer();

                    if (u9.plugins.email) {
                        u9.plugins.email.isAvailable(function (isAvailable) {
                            if (isAvailable) {
                                q.resolve();
                            } else {
                                q.reject();
                            }
                        });
                    } else {
                        q.reject();
                    }

                    return q.promise;
                },
                open: function (properties) {
                    var q = $q.defer();

                    if (u9.plugins.email) {
                        u9.plugins.email.open(properties, function () {
                            q.reject(); // user closed email composer
                        });
                    } else {
                        q.reject();
                    }

                    return q.promise;
                },

                addAlias: function (app, schema) {
                    if (u9.plugins.email) {
                        u9.plugins.email.addAlias(app, schema);
                    }
                }
            };
        }]);
});